// 导入模块 Import modules
import { Administrators } from './admin';

/**
 * @zh 根据消息进行管理员权限的执行
 * @param entity 管理员实体
 * @param message 消息
 * @en Run administrators permission based on a message
 * @param entity Administrator entity
 * @param message Message
 */
function admin(entity: GamePlayerEntity, message: string): string {
    var list = message.split(' ');
    for (let i = 0; i < list.length; i++) {
        list[i] = list[i].toLowerCase();
    }
    if (list[0] === '/code' || list[0] === '\\code') {
        const code = message.slice(6);
        try {
            const result = eval(code);
            try {
                return '~>' + JSON.stringify(result);
            } catch (e) {
                console.warn(e);
                return '~>' + String(result);
            }
        } catch (e) {
            return e as string;
        }
    } else if (list[0] === '/admin' || list[0] === '\\admin') {
        if (list[1] === '-l' || list[1] === '--list') {
            let text = '';
            for (let i = 0; i < Administrators.length; i++) {
                text += `\n@${Administrators[i].name} (${Administrators[i].userId}): ${String(Administrators[i].job)}`;
            }
            return text;
        }
    }
    return `无法识别指令 ${message}`;
}

// 导出模块 Export modules
export { admin };
